package Frame;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Vector;

import javax.swing.JPanel;

import Function.Utils.FCFileCopyToMaterial;

public class FCMaterailMap extends JPanel {
	private Image entityicon = Toolkit.getDefaultToolkit().getImage("./image/entityIcon.png");
	private Image im;
	private int x = 0, y = 0;
	private FCJtable t, t2;
	private Color c1 = new Color(240, 220, 220);
	private int w = 0, h = 0;
	private String pngurl;
	private String[][] qw;
	private Image[][] as;
	private boolean isEntitySet = false;
	private FCFileCopyToMaterial filecopy = new FCFileCopyToMaterial();

	public FCMaterailMap(FCJtable t, FCJtable t2) {
		this.t = t;
		this.t2 = t2;
		addMouseMotionListener(new MouseMotionListener() {
			@Override
			public void mouseMoved(MouseEvent e) {
				x = e.getX();
				y = e.getY();
			}

			@Override
			public void mouseDragged(MouseEvent e) {
			}
		});
		addMouseListener(new MouseListener() {
			private int y = 0, x = 0;

			@Override
			public void mouseReleased(MouseEvent e) {
				int yy = (e.getX() - 1) / 32, xx = (e.getY() - 1) / 32;
				for (int i = x; i <= xx; i++)
					for (int j = y; j <= yy; j++) {
						if (i > -1 && i < h - 2 && j > -1 && j < w - 2) {
							if (isEntitySet) {
								new FCEditBlockEntity(i, j, t2);
							} else {
								qw[i][j] = pngurl;
								if (qw[i][j] != null)
									as[i][j] = Toolkit.getDefaultToolkit().getImage(qw[i][j]);
								else
									as[i][j] = null;
								setBlockPngUrlVecter();
								setFirstAndEndCoor();
							}
						}
					}
			}

			@Override
			public void mousePressed(MouseEvent e) {
				y = (e.getX() - 1) / 32;
				x = (e.getY() - 1) / 32;
			}

			@Override
			public void mouseExited(MouseEvent e) {
			}

			@Override
			public void mouseEntered(MouseEvent e) {
			}

			@Override
			public void mouseClicked(MouseEvent e) {

			}
		});
	}

	@Override
	public void setBounds(int x, int y, int width, int height) {
		super.setBounds(x, y, width, height);
		w = getWidth() / 32;
		h = getHeight() / 32;
		qw = new String[w - 1][h - 1];
		as = new Image[w - 1][h - 1];
	}

	@Override
	protected void paintComponent(Graphics g) {
		g.setColor(Color.white);
		g.fillRect(0, 0, getWidth(), getHeight());
		g.setColor(c1);
		for (int i = 0; i < w - 1; i++)
			g.drawLine(33 + i * 32, 1, 33 + i * 32, getHeight() - 2);
		for (int i = 0; i < h - 1; i++)
			g.drawLine(1, 33 + i * 32, getWidth() - 2, 33 + i * 32);
		g.setColor(Color.black);
		g.drawLine(0, 0, 0, getHeight() - 1);
		g.drawLine(0, 0, getWidth() - 1, 0);
		g.drawLine(getWidth() - 1, getHeight() - 1, getWidth() - 1, 0);
		g.drawLine(getWidth() - 1, getHeight() - 1, 0, getHeight() - 1);

		for (int i = 0; i < w - 1; i++)
			for (int j = 0; j < h - 1; j++)
				if (as[i][j] != null)
					g.drawImage(as[i][j], 1 + 32 * j, 1 + 32 * i, 32, 32, this);
		if (im != null)
			if (isEntitySet)
				g.drawImage(im, x, y - 16, 16, 16, this);
			else
				g.drawImage(im, x - 16, y - 16, 32, 32, this);
	}

	@Override
	public void repaint() {
		String ss;
		super.repaint();
		if (t != null) {
			ss = t.getSeletePng();
			if (ss != null) {
				isEntitySet = false;
				if (!ss.equals(pngurl)) {
					pngurl = ss;
					this.im = Toolkit.getDefaultToolkit().getImage(pngurl);
				}
			} else if (!isEntitySet) {
				im = null;
				pngurl = null;
			}
		}
	}

	private Vector<String> qwe = new Vector<String>();

	protected void setBlockPngUrlVecter() {
		qwe.clear();
		for (int i = 0; i < qw.length; i++)
			for (int j = 0; j < qw[0].length; j++)
				if (qwe.indexOf(qw[i][j]) < 0 && qw[i][j] != null) {
					qwe.add(qw[i][j]);
				}
	}

	private int firstx = -1, firsty = -1, endx = -1, endy = -1;

	protected void setFirstAndEndCoor() {
		for (int i = 0; i < qw.length; i++)
			for (int j = 0; j < qw[i].length; j++)
				if (firstx < 0) {
					if (qw[i][j] != null) {
						firstx = i;
						endx = i;
					}
				} else {
					if (qw[i][j] != null && i > endx)
						endx = i;
				}
		for (int j = 0; j < qw[0].length; j++)
			for (int i = 0; i < qw.length; i++)
				if (firsty < 0) {
					if (qw[i][j] != null) {
						firsty = j;
						endy = j;
					}
				} else {
					if (qw[i][j] != null && j > endy)
						endy = j;
				}
	}

	// 获取方块拼接的模型
	public int[][] getBlockModelArray() {
		int[][] a = new int[endx - firstx + 1][endy - firsty + 1];
		for (int i = 0; i < a.length; i++)
			for (int j = 0; j < a[0].length; j++)
				a[i][j] = -1;
		for (int i = firstx; i <= endx; i++)
			for (int j = firsty; j < endy + 1; j++)
				a[i - firstx][j - firsty] = qwe.indexOf(qw[i][j]);
		return a;
	}

	// 获取所需的材质
	public String[] getBlockPngUrlArray(boolean isnew) {
		String[] asd = new String[qwe.size()];
		for (int i = 0; i < asd.length; i++)
			asd[i] = (isnew) ? filecopy.copyPngToMaterialPng(0, qwe.get(i)) : qwe.get(i);
		return asd;
	}

	// 获取方块实际起始坐标与终止坐标
	public int[] getBlockFirstAndEndCoorArray() {
		return new int[] { firstx, firsty, endx, endy };
	}

	// 开启或关闭实体选择功能
	public void setEntutyOpenOrClose() {
		isEntitySet = !isEntitySet;
		if (isEntitySet)
			im = entityicon;
		else
			im = null;
		pngurl = null;
	}

	// 加载选中方块数据
	public void loadSelectBlockData(String pngurl[], int[][] model) {
		for (int i = 0; i < model.length; i++)
			for (int j = 0; j < model[0].length; j++) {
				qw[i][j] = pngurl[model[i][j]];
				if (qw[i][j] != null)
					as[i][j] = Toolkit.getDefaultToolkit().getImage(qw[i][j]);
			}
		repaint();
	}
}
